<%@page import="study.top.jsp.MultipartBox.FileInfo"%>
<%@page import="java.util.ArrayList"%>
<%@page import="study.top.mydb.items.BoardItem"%>
<%@page import="study.top.mydb.Board"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page session="true" %>
<%@ include file="dbcon.jsp" %>

<!DOCTYPE html>
<html lang="ko">
	<%@ include file="topHead.jsp" %>
	<body>
	<%@ include file="topcategory.jsp" %>
	
<%
	db.connect();
	if(mem_seq <= 0) {
		web.goPage(null, "수정할 수 없습니다.");
		return;
	}

	int seq = web.getInt("seq");
	
	if (seq < 0) {
		web.goPage(null, "파라미터가 없습니다.");
		db.disconnect();
		return;
	}

	Board board = new Board();
	BoardItem b_item = board.read(seq);
	db.disconnect();
	
	if (b_item == null) {
		web.goPage(null, "게시물 데이터를 불러오는데 실패했습니다.");
		return;
	}
%>
	<div class="top-tab top-hidden-xs">
		<%@include file="tab.jsp"%>
	</div>
		<div class="container"style="width: 70%">
		
			<div class="page-header">
				<h3>Q&A 수정 - <small>수정할 부분을 작성하세요.</small></h3>
			</div>
			
			<form class="form-horizontal" id="boardedit" role="form" action="uBrdQAedit_ok2.jsp" enctype="multipart/form-data" method="post">
				<input type="hidden" name="seq" value="<%=seq%>" />
				<fieldset>
					
					<!-- 입력양식 -->
					<div class="form-group">
						<label for="title" class="col-md-2 control-label">글 제목</label>
						<div class="col-md-10">
							<input type="text" class="form-control" id="title" name="title" placeholder="제목을 입력하세요." value="<%=b_item.getTitle()%>">
						</div>
					</div>
					<!--// 입력양식 -->
					
					<!-- 입력양식 -->
					<div class="form-group">
						<label for="memberID" class="col-md-2 control-label">작성자</label>
						<div class="col-md-10">
							<input type="text" class="form-control" id="memberID" name="memberID" value="<%=b_item.getMemberID() %>" disabled="disabled">
						</div>
					</div>
					<!--// 입력양식 -->
					
					
					
					<!-- 입력양식 -->
					<div class="form-group">
						<label for="content" class="col-md-2 control-label">내용</label>
						<div class="col-md-10">
							<textarea id="content" name="content" class="form-control ckeditor" rows="5" ><%=b_item.getContent()%></textarea>
						</div>
					</div>
					<!--// 입력양식 -->
					
					<!-- 입력양식 -->
					<div class="form-group">
						<label for="file" class="col-md-2 control-label"></label>
						<div id="fileadd" class="col-md-10">							
							<button type="button" id="mybtn" class="btn top-btn-custom">이미지 항목 추가</button>
							<button type='button' id='mybtn2' class='btn btn-default'>이미지 항목 삭제</button>					
						</div>
						
						<!--  삭제를 위하여 기존에 업로드된 파일 목록을 체크박스로 출력  -->
						<%
							ArrayList <FileInfo> imageList = b_item.getImageList();
								if(imageList != null) {
								for(int i=0; i<imageList.size(); i++) {
									FileInfo info = imageList.get(i);
								
						%>
						<div class="col-md-10 col-md-offset-2">
							<div class="checkbox">
								<label type="checkbox" value="파일의 일련번호">
								<input type="checkbox" name="delete_file" value="<%=info.getFileSaveName() %>" />
								<%=info.getFileOrgName() %>(삭제)
								</label>
							</div>
						</div>
						<%
								}
							}
						%>
					</div>
					<!--// 입력양식 -->
					
					<!-- 입력양식 -->
					<div class="form-group">
						<div class="col-md-offset-4 col-md-8">
							<div class="row">
								<div class="col-xs-3">
									<button type="submit" class="btn top-btn-custom btn-block">저장</button>	
								</div>
								<div class="col-xs-3">							
									<a href="uBrdQAView.jsp?seq=<%=seq %>" class="btn btn-default btn-block">취소</a>
								</div>
							</div>
						</div>
					</div>
					<!--// 입력양식 -->
				</fieldset>
			</form>

		</div>
	
		<div class="top-hidden-xs">
			<%@ include file="topBottom.jsp" %>
		</div>
			
			
				<!-- Javascript -->
				<script src="assets/js/string.js"></script>
				
				<script src="assets/ckeditor/ckeditor/ckeditor.js"></script>
				<script type="text/javascript">
				  CKEDITOR.replace( 'content',{
    				
					 uiColor: '#ffffff',
					 toolbar:
    				[				
    				 	['Styles','Format','Font','FontSize'],    				 
      					['Bold','-', 'Italic'],
      					['NumberedList']
    				]
  				});
				  
				  $(function(){
						
						$('#mybtn').click(function() {
							
							$("#fileadd").append($("<input type='file' class='form-control' id='file' name='file' multiple />"));
							
																													
							
						});
						
						$('#mybtn2').click(function(){
							
							$("#file:last").remove();
						});
						
											
						$("#boardedit").submit(function(){
							
							if (!$("input[name='title']").val()
									.isValue()) {
								alert("제목을 입력하세요.");
								$("input[name='title']").focus();
								return false;
							}
							if (!$("textarea[name='content']").val()
									.isValue()) {
								alert("내용을 입력하세요.");
								$("textarea[name='content']").focus();
								return false;
							}
							if (!$("input[name='file']").val()
									.isValue()) {
								alert("이미지를 올려주세요.");
								$("input[name='file']").focus();
								return false;
							}
							
							
							
						});
					});
				  
				</script>
				
	</body>
</html>